﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using CheckLift.Common.Impl;
using CheckLift.Common.Intf;
using System.Data.Linq;

namespace CheckLift.Linq
{
    public class ReportDataImpl : BaseData
    {
        public ReportDataImpl()
        {
            DataLoadOptions options = new DataLoadOptions();

            options.LoadWith<Report>(r => r.Lift);
            options.LoadWith<Lift>(l => l.Customer);
            options.LoadWith<Lift>(l => l.ServiceCompany);
            dc.LoadOptions = options;
        }

        public ObservableCollection<IReport> GetReportsByUser(Guid userId)
        {            

            var reports = CompiledQuery.Compile ((CheckLiftDataDataContext dcEx, Guid userIdEx) =>
                            from report in dcEx.Reports
                            orderby report.ExternalId
                            where report.Lift.Customer.Company.User_Company_Links.Any(p => p.UserId == userIdEx) 
                          select report); 
            return Convert<IReport>(reports(dc,userId));
        }

        public ReportData GetReportData(Guid reportId)
        {
            var report = (from report_data in dc.ReportDatas
                         where report_data.ReportId.Equals(reportId)
                          select report_data).FirstOrDefault<ReportData>();
            return report;
        }

        public Dictionary<string,ReportPropertiesVal> GetReportPropertyValues(Guid reportId)
        {
            Dictionary<string, ReportPropertiesVal> res = new Dictionary<string, ReportPropertiesVal>();
            var rep_prop =
                from report_properties in dc.Properties
                join report_values in dc.ReportValues on report_properties.Id equals report_values.PropertyId
                where report_values.ReportId == reportId
                select new ReportPropertiesVal((IProperty)report_properties)
                {
                    Value = report_values.PropertyValue
                };
            foreach (var item in rep_prop)
            {
                res.Add((item as ReportPropertiesVal).Name, item);
            }

            return res;
        }       

        public IReport GetReport(Guid reportId)
        {
            var reports = (from report in dc.Reports
                          //join company in dc.Companies on report.Lift.Customer.CompanyId equals company.Id
                          //join customer in dc.Customers on report.Lift.CustomerId equals customer.Id
                          //join user_company in dc.User_Company_Links on company.Id equals user_company.CompanyId
                          //join service_company in dc.ServiceCompanies on report.Lift.ServiceId equals service_company.Id
                          orderby report.ExternalId
                           where report.Id == reportId
                          select report 
                          //new ReportView(report)
                          //{
                          //    CompanyId = company.Id,
                          //    Customer_ExternalId = customer.ExternalId,
                          //    Lift_ExternalId = report.Lift.ExternalId,
                          //    Lift_SubExternalId = report.Lift.SubExternalId,
                          //    Lift_ServiceId = report.Lift.ServiceId,
                          //    ServiceName = service_company.Name,
                          //}
                          ).FirstOrDefault<IReport>();


            return reports;
        }


        public void SaveReportInfo(Guid reportId, Dictionary<string, ReportPropertiesVal> propList)
        {
            
        }
                
    }
}
